﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.BLL;
using Garlic.CC.BaseClass;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.Utility;

public partial class SystemManagement_UserEdit : EditBasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            base.ControlToBind = this.txtAccountName.NamingContainer;

            if (!IsPostBack)
            {
                string userID = Request.QueryString["UserID"];
                if (!string.IsNullOrEmpty(userID))
                {
                    UserInfo user = UserManager.GetInfo(int.Parse(userID));
                    HdUserID.Value = user.User_id.ToString();
                    txtAccountName.Text = user.User_name;
                    txtAccountName.ReadOnly = true;
                    SetUserTypeSelect(user.User_type);
                    txtEmployeeName.Text = user.Employee_name;
                    txtMobile.Text = user.Mobile_phone;
                    txtOfficePhone.Text = user.Office_phone;
                    txtCompany.Text = user.Company;
                    txtDepartment.Text = user.Department;
                }

                base.InitPage();
            }
        }
        catch (Exception ex)
        {
            base.HandleException(ex);
        }

    }

    


    private void SetUserTypeSelect(int userType)
    {
        //switch (userType)
        //{
        //    case 0:
        //        ddlUserType.SelectedValue = "0";
        //        break;
        //    case 1:
        //        ddlUserType.SelectedValue = "1";
        //        break;
        //    case 2:
        //        ddlUserType.SelectedValue = "2";
        //        break;
        //    case 3:
        //        ddlUserType.SelectedValue = "3";
        //        break;
        //}
    }

    protected void GeneralToolBar1_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
            UserInfo user;

            if (string.IsNullOrEmpty(HdUserID.Value))
            {
                IUser u = UserManager.GetUserByName(txtAccountName.Text);
                if (u != null)
                {
                    this.Message = "人员ID重复，请确认。";
                    return;
                }
                user = new UserInfo();
            }
            else
            {
                user = UserManager.GetInfo(int.Parse(HdUserID.Value));
            }

            user.User_name = txtAccountName.Text;
            user.Employee_name = txtEmployeeName.Text;
            user.Mobile_phone = txtMobile.Text;
            user.Office_phone = txtOfficePhone.Text;
            user.Status = 1;
            user.Company = txtCompany.Text.Trim();
            user.Department = txtDepartment.Text.Trim();

            if (string.IsNullOrEmpty(HdUserID.Value))
            {
                user.Create_user = CurrentUser.UserID;
                user.Create_time = DateTime.Now;
                user.Modify_user = CurrentUser.UserID;
                user.Modify_time = user.Create_time;
                //Modify by Andy Liu Date: 2009-05-11
                //Note:密码过期时间取配置的90天
                //user.Password_expire_time = DateTime.Now.AddYears(50);
                int expireDay = 90;   //默认取90天
                SysConfigInfo sys = SysConfigManager.GetInfo("PasswordExpireTime");
                if (sys != null)
                {
                    expireDay = int.Parse(sys.Parameter_value);
                }
                user.Password_expire_time = DateTime.Now.AddDays(expireDay);

                user.Fail_login = 0;
                string password;
                if (user.User_type == 0)
                    password = txtJITDDPassword.Text;
                else if (user.User_type == 1)
                    password = txtEPSPassword.Text;
                else if (user.User_type == 2)
                    password = txtRDCPassword.Text;
                else if (user.User_type == 3)
                    password = txtSupplierPassword.Text;
                else
                    password = "Pass1234";
                user.Password = user.Password1 = user.Password2 = user.Password3 = CryptTools.Encrypt(password, CryptConstants.PassKey);
            }
            else
            {
                user.Modify_user = CurrentUser.UserID;
                user.Modify_time = DateTime.Now;

                //bool flag = UserManager.ProcessModifyPassword(ref user, txtPassword.Text);
            }

            if (string.IsNullOrEmpty(HdUserID.Value))
            {
                user.User_id = SequenceManager.GetNextVal("UserID");
                UserManager.Add(user);
            }
            else
            {
                UserManager.Update(user);
            }
            this.CloseVirtualWindow(true);
        

        //this.ShowMessage("成功。");
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }
}
